﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Configuration.Provider;
using DbDoc.BusinessManager;

using DbDoc.BusinessObjects;
using System.Windows;
using System.Xml.Linq;
using DbDoc.Provider.Source;
using DbDoc.Provider.Persistence;
using System.Collections.ObjectModel;


namespace DbDoc.PowerDoc.ViewModel
{
    public class SettingsViewModel : ViewModelBase
    {
        private bool _DisplaySystemObjects;
        private DelegateCommand _SaveSettingsCommand;
        private string _SourceConnectionInfo;
        private string _ExcludedDatabases;
        private string _PersistenceConnectionInfo;
        private string _PersistenceStorageName;
        private ProviderCollection _PersistenceProviders;
        private PersistenceProvider _SelectedPersistenceProvider;
        private ProviderCollection _SourceProviders;
        private SourceProvider _SelectedSourceProvider;
        private AppUser _LoggedUser;
        private bool _AutoSave = true;
        private int _AutoSaveMinutes = 3;
        private bool _SynchronizeOnStartup;
        private bool _EnableUserLogin;
        private bool _SynchronizeSystemObjects;
        private ObservableCollection<AppUser> _AppUsers;

        public bool SynchronizeSystemObjects
        {
            get { return _SynchronizeSystemObjects; }
            set
            {
                _SynchronizeSystemObjects = value;
                OnPropertyChanged("SynchronizeSystemObjects");
            }
        }

        public bool IsAdmin
        {
            get { return LoggedUser.IsAdmin; }
        }

        public event EventHandler Changed;

        public int AutoSaveMinutes
        {
            get { return _AutoSaveMinutes; }
            set
            {
                _AutoSaveMinutes = value;
                OnPropertyChanged("AutoSaveMinutes");
            }
        }

        public bool SynchronizeOnStartup
        {
            get { return _SynchronizeOnStartup; }
            set
            {
                _SynchronizeOnStartup = value;
                OnPropertyChanged("SynchronizeOnStartup");
            }
        }

        public bool AutoSave
        {
            get { return _AutoSave; }
            set
            {
                _AutoSave = value;
                OnPropertyChanged("AutoSave");
            }
        }

        public AppUser LoggedUser
        {
            get { return _LoggedUser; }
            set
            {
                _LoggedUser = value;
                OnPropertyChanged("LoggedUser");
            }
        }

        public SourceProvider SelectedSourceProvider
        {
            get { return _SelectedSourceProvider; }
            set
            {
                _SelectedSourceProvider = value;
                OnPropertyChanged("SelectedSourceProvider");
            }
        }

        public ProviderCollection SourceProviders
        {
            get { return _SourceProviders; }
            set
            {
                _SourceProviders = value;
                OnPropertyChanged("SourceProviders");
            }
        }

        public PersistenceProvider SelectedPersistenceProvider
        {
            get { return _SelectedPersistenceProvider; }
            set
            {
                _SelectedPersistenceProvider = value;
                OnPropertyChanged("SelectedPersistenceProvider");
            }
        }

        public ProviderCollection PersistenceProviders
        {
            get { return _PersistenceProviders; }
            set
            {
                _PersistenceProviders = value;
                OnPropertyChanged("PersistenceProviders");
            }
        }
        public string PersistenceStorageName
        {
            get { return _PersistenceStorageName; }
            set
            {
                _PersistenceStorageName = value;
                OnPropertyChanged("PersistenceStorageName");
            }
        }

        public string PersistenceConnectionInfo
        {
            get { return _PersistenceConnectionInfo; }
            set
            {
                _PersistenceConnectionInfo = value;
                OnPropertyChanged("PersistenceConnectionInfo");
            }
        }

        public string ExcludedDatabases
        {
            get { return _ExcludedDatabases; }
            set
            {
                _ExcludedDatabases = value;
                OnPropertyChanged("ExcludedDatabases");
            }
        }

        public string SourceConnectionInfo
        {
            get { return _SourceConnectionInfo; }
            set
            {
                _SourceConnectionInfo = value;
                OnPropertyChanged("SourceConnectionInfo");
            }
        }

        public bool DisplaySystemObjects
        {
            get { return _DisplaySystemObjects; }
            set
            {
                _DisplaySystemObjects = value;
                OnPropertyChanged("DisplaySystemObjects");
            }
        }

        public bool EnableUserLogin
        {
            get { return _EnableUserLogin; }
            set
            {
                _EnableUserLogin = value;
                OnPropertyChanged("EnableUserLogin");
            }
        }

        public ObservableCollection<AppUser> AppUsers
        {
            get { return _AppUsers; }
            set
            {
                _AppUsers = value;
                OnPropertyChanged("AppUsers");
            }
        }


        public DelegateCommand SaveSettingsCommand
        {
            get
            {
                if (_SaveSettingsCommand == null)
                    _SaveSettingsCommand = new DelegateCommand(param => OnSaveSettingsExecuted());
                return _SaveSettingsCommand;
            }
            set { _SaveSettingsCommand = value; }
        }

        private void OnSaveSettingsExecuted()
        {
            XDocument settingsDoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Settings.xml");
            var settings = from s in settingsDoc.Descendants("Setting")
                           select s;
            foreach (XElement element in settings)
            {
                if (element.Attribute("Key").Value == "AutoSave")
                    element.SetAttributeValue("Value", AutoSave);
                if (element.Attribute("Key").Value == "AutoSaveMinutes")
                    element.SetAttributeValue("Value", AutoSaveMinutes);
                if (element.Attribute("Key").Value == "DisplaySystemObjects")
                    element.SetAttributeValue("Value", DisplaySystemObjects);
            }
            settingsDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Settings.xml");
            SettingsManager.UserLoginRequired(EnableUserLogin);
            SettingsManager.SaveUsers(AppUsers);            
            PopupInfo("Settings saved successfully.", "Settings Saved.");

            if (Changed != null)
                Changed(this, EventArgs.Empty);
        }

        public SettingsViewModel(AppUser loggedUser, Action<string, string> popupInfo, Action<string, string> popupError)
        {
            PopupInfo = popupInfo;
            PopupError = popupError;
            LoggedUser = loggedUser;
            PersistenceConnectionInfo = DatabaseManager.PersistenceConnectionInfo;
            PersistenceStorageName = DatabaseManager.PersistenceStorageName;
            ExcludedDatabases = DatabaseManager.SourceExcludedDatabases;
            PersistenceProviders = SettingsManager.GetPersistenceProviders();
            foreach (PersistenceProvider provider in PersistenceProviderManager.Providers)
                if (provider == SettingsManager.GetSelectedProvider())
                    SelectedPersistenceProvider = provider;

            SelectedSourceProvider = SourceProviderManager.Provider;
            EnableUserLogin = SettingsManager.RequireLogin();
            AppUsers = new ObservableCollection<AppUser>(SettingsManager.GetAllUsers());
            LoadSettings();
        }


        private void LoadSettings()
        {
            XDocument settingsDoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Settings.xml");
            var settings = from s in settingsDoc.Descendants("Setting")
                           select s;
            foreach (XElement element in settings)
            {
                if (element.Attribute("Key").Value == "AutoSave")
                    AutoSave = bool.Parse(element.Attribute("Value").Value);
                if (element.Attribute("Key").Value == "AutoSaveMinutes")
                    AutoSaveMinutes = Int32.Parse(element.Attribute("Value").Value);
                if (element.Attribute("Key").Value == "DisplaySystemObjects")
                    DisplaySystemObjects = Boolean.Parse(element.Attribute("Value").Value);
            }
        }



    }
}
